/*
 * @Author: 杰编程 2306248989@qq.com
 * @Date: 2023-07-21 18:53:47
 * @LastEditors: 杰编程 2306248989@qq.com
 * @LastEditTime: 2023-07-21 19:42:12
 * @FilePath: /lesson17/myprocess.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <iostream>

using namespace std;

#include <unistd.h>
#include <signal.h>
#include<sys/wait.h>

int count = 0;

void myhandler(int signo)
{
    cout << "get a signo:" << signo << endl;
}

int main()
{

    pid_t id=fork();
    if(id==0)
    {
        sleep(2);
        cout << "野指针问题 ... here" << endl;
        cout << "野指针问题 ... here" << endl;
        cout << "野指针问题 ... here" << endl;

        int *p = nullptr;
        // p = 100; // 1
        *p = 100; // 2, 野指针问题

        cout << "野指针问题 ... here" << endl;
        cout << "野指针问题 ... here" << endl;
        cout << "野指针问题 ... here" << endl;
        cout << "野指针问题 ... here" << endl;
        exit(0);
    }
    int status=0;
    waitpid(id,&status,0);
    cout<<"exit code: "<<((status>>8)&0xFF)<<endl;
    cout<<"eixt signal: "<<(status&0x7F)<<endl;
    cout<<"exit dump flag: "<<((status>>7)&0x1)<<endl;


    // // signal(SIGSEGV, myhandler);
    // cout << "nullptr error is here!" << endl;
    // cout << "nullptr error is here!" << endl;

    // char *ptr = nullptr;
    // cout << *ptr << endl;
    // cout << "nullptr error is here!" << endl;
    // cout << "nullptr error is here!" << endl;

    // signal(SIGFPE, myhandler);
    // int a=0;
    // a/=0;
    // cout<<"divide 0 is here！"<<endl;

    // signal(SIGALRM, myhandler);
    // alarm(1);
    // while (true)
    // {
    //     count++;
    // }

    // signal(SIGABRT, myhandler);
    //  while (true)
    //  {
    //      cout << "I am a process, I am waiting singal!:" << getpid() << endl;
    //      abort();
    //      sleep(1);
    //  }

    return 0;
}